/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.large.service;

import com.fzhucc.modules.appletShow.repository.AppletShowRepository;
import com.fzhucc.modules.expert.repository.ExpertRepository;
import com.fzhucc.modules.financing.domain.Financing;
import com.fzhucc.modules.financing.repository.FinancingRepository;
import com.fzhucc.modules.innovativeEnterprise.repository.InnovativeEnterpriseRepository;
import com.fzhucc.modules.intellectualProperty.repository.IntellectualPropertyRepository;
import com.fzhucc.modules.large.dto.*;
import com.fzhucc.modules.mnt.util.DataTypeEnum;
import com.fzhucc.modules.pms.repository.ProjectRepository;
import com.fzhucc.modules.thinktank.repository.CapitalRepository;
import com.fzhucc.modules.thinktank.repository.CompanyRepository;
import com.fzhucc.modules.thinktank.repository.LeaderRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2022-12-06
 **/
@Service
@RequiredArgsConstructor
public class LargeService {

    private final ExpertRepository expertRepository;

    private final ProjectRepository projectRepository;

    private final LeaderRepository leaderRepository;

    private final CompanyRepository companyRepository;

    private final CapitalRepository capitalRepository;

    private final AppletShowRepository appletShowRepository;

    private final InnovativeEnterpriseRepository innovativeEnterpriseRepository;

    private final IntellectualPropertyRepository intellectualPropertyRepository;

    private final FinancingRepository financingRepository;

    public LargeXcxDto getXcxData() {
        LargeXcxDto dto = new LargeXcxDto();
        Integer countExperts = 0;
        //劳模工匠
        int a1 = expertRepository.findByType("劳模工匠");
        //财税专家
        int a2 = expertRepository.findByType("财税专家");
        //行业专家
        int a3 = expertRepository.findByType("行业专家");
        //法律专家
        int a4 = expertRepository.findByType("法律专家");
        //金融专家
        int a5 = expertRepository.findByType("金融专家");
        //投资人
        int a6 = expertRepository.findByType("投资人");
        //创业导师
        int a7 = expertRepository.findByType("创业导师");
        //知识产权专家
        int a8 = expertRepository.findByType("知识产权专家");
        //管理专家
        int a9 = expertRepository.findByType("管理专家");
        countExperts = countExperts + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
        dto.setCountExperts(countExperts);


        Integer countProjects = 0;
        //福清三创大赛
        int fq_res = projectRepository.findBySource("福清三创大赛");
        //福州三创大赛
        int res = projectRepository.findLikeSource("三创");
        int dyj_res = projectRepository.findBySource("第一届大赛");
        int fqsc = (res - fq_res) + dyj_res;
        //五小项目收录
        //int wx = projectRepository.findLikeSource("五小");
        //wx = wx + 100;
        int wx = 3319;
        int allNormal = projectRepository.findAllNormal();
        //科创载体
        int kc = allNormal - fq_res - fqsc - wx - 1;
        countProjects = countProjects + fq_res + fqsc + wx + kc;
        dto.setCountProjects(countProjects);



        int allszscq = intellectualPropertyRepository.findCounts();//知识产权数
        dto.setIntellectuals(allszscq);

        //融资需求企业(团队)
        //融资额 /10000 变成亿元
        int allrz = 0;
        List<Financing> list1 = financingRepository.findAllCountByType("融资企业");
        if (list1 != null && list1.size() > 0) {
            for (Financing financing : list1) {
                Integer amount = financing.getAmount();
                if (amount != null) {
                    allrz = allrz + amount;
                }
            }
        }
        if (allrz > 0) {
            String str = Integer.toString(allrz);
            BigDecimal bigDecimal = new BigDecimal(str);
            // 转换为万元（除以10000）
            BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
            // 保留两位小数
            DecimalFormat formater = new DecimalFormat("0.00");
            // 四舍五入
            formater.setRoundingMode(RoundingMode.HALF_UP);
            // 格式化完成之后得出结果
            String formatNum = formater.format(decimal);
            dto.setMoney(formatNum);
        } else {
            dto.setMoney("0");
        }
        return dto;
    }

    public LargeExpertDto getExpert() {
        LargeExpertDto largeExpertDto = new LargeExpertDto();
        //劳模工匠
        largeExpertDto.setModelWorker(expertRepository.findByType("劳模工匠"));
        //财税专家
        largeExpertDto.setFinanceTax(expertRepository.findByType("财税专家"));
        //行业专家
        largeExpertDto.setIndustry(expertRepository.findByType("行业专家"));
        //法律专家
        largeExpertDto.setLegal(expertRepository.findByType("法律专家"));
        //金融专家
        largeExpertDto.setFinancial(expertRepository.findByType("金融专家"));
        //投资人
        largeExpertDto.setInvestor(expertRepository.findByType("投资人"));
        //创业导师
        largeExpertDto.setMentor(expertRepository.findByType("创业导师"));
        //知识产权专家
        largeExpertDto.setIntellectual(expertRepository.findByType("知识产权专家"));
        //管理专家
        largeExpertDto.setManagement(expertRepository.findByType("管理专家"));
        return largeExpertDto;
    }

    public ProjectLibraryDto getProjectLibrary() {
        ProjectLibraryDto projectLibraryDto = new ProjectLibraryDto();
        //职创项目库  = 优秀项目库  + 重点项目库 + 职创项目库
        //优秀项目库 =  优秀项目库 + 重点项目
        //重点项目
        int CountsZc = projectRepository.findByType(1); //职创项目库
        int CountsYx = projectRepository.findByType(2); //优秀项目库
        int CountsZd = projectRepository.findByType(3); //重点项目库
        projectLibraryDto.setJob(CountsZc + CountsYx + CountsZd);
        projectLibraryDto.setExcellent(CountsYx + CountsZd);
        projectLibraryDto.setKey(CountsZd);
        return projectLibraryDto;
    }

    public ProjectSourceDto getProjectSource() {
        ProjectSourceDto projectSourceDto = new ProjectSourceDto();
        //福清三创大赛
        int fq_res = projectRepository.findBySource("福清三创大赛");
        projectSourceDto.setFq(fq_res);
        //福州三创大赛
        int res = projectRepository.findLikeSource("三创");
        int dyj_res = projectRepository.findBySource("第一届大赛");
        int fqsc = (res - fq_res) + dyj_res;
        projectSourceDto.setFz(fqsc);
        //五小项目收录
        //int wx = projectRepository.findLikeSource("五小");
        //wx = wx + 100;
        int wx = 3319;
        projectSourceDto.setWx(wx);

        int allNormal = projectRepository.findAllNormal();
        //科创载体
        projectSourceDto.setCarrier(allNormal - fq_res - fqsc - wx - 1);
        return projectSourceDto;
    }

    public ThinkTanksDto getThinkTanks() {
        ThinkTanksDto thinkTanksDto = new ThinkTanksDto();
        //产业龙头
        thinkTanksDto.setIndustrialLeader(leaderRepository.findCount());
        //产业资本
        thinkTanksDto.setIndustrialCapital(capitalRepository.findCount());
        //上市公司
        thinkTanksDto.setListedCompany(companyRepository.findCount());
        return thinkTanksDto;
    }

    public ResultsTransferDto getResultsTransfer() {
        ResultsTransferDto resultsTransferDto = new ResultsTransferDto();
        int sshb = innovativeEnterpriseRepository.findCountByType("上市后备");
        int zjtx = innovativeEnterpriseRepository.findCountByType("专精特新");
        int gxjs = innovativeEnterpriseRepository.findCountByType("高新技术");
        int allszscq = intellectualPropertyRepository.findCounts();//知识产权数
        resultsTransferDto.setBackup(sshb);//上市后备企业
        resultsTransferDto.setSpecial(zjtx);//专精特新企业
        resultsTransferDto.setHigh(gxjs);//高新技术企业
        resultsTransferDto.setIntellectualProperty(allszscq);//知识产权
        //产业对接
        //融资服务
        //知识产权
        return resultsTransferDto;
    }

    public LargeServiceDto getLargeService() {
        LargeServiceDto largeServiceDto = new LargeServiceDto();
        //第三方服务商数量
        //服务企业数
        return largeServiceDto;
    }

    public FinanceAbutmentDto getFinanceAbutmentDto() {
        FinanceAbutmentDto financeAbutmentDto = new FinanceAbutmentDto();
        //融资企业(团队)
        int rzqy = financingRepository.findCountByType("融资企业");
        int rzxq = financingRepository.findCountByType("融资需求");
        financeAbutmentDto.setTeam(rzqy);
        financeAbutmentDto.setNeedsTeam(rzxq);
        //融资需求企业(团队)
        //融资额 /10000 变成亿元
        int allrz = 0;
        int allxq = 0;
        List<Financing> list1 = financingRepository.findAllCountByType("融资企业");
        if (list1 != null && list1.size() > 0) {
            for (Financing financing : list1) {
                Integer amount = financing.getAmount();
                if (amount != null) {
                    allrz = allrz + amount;
                }
            }
        }

        List<Financing> list2 = financingRepository.findAllCountByType("融资需求");
        if (list2 != null && list2.size() > 0) {
            for (Financing financing : list2) {
                Integer needs = financing.getNeeds();
                if (needs != null) {
                    allxq = allxq + needs;
                }
            }
        }

        if (allrz > 0) {
            String str = Integer.toString(allrz);
            BigDecimal bigDecimal = new BigDecimal(str);
            // 转换为万元（除以10000）
            BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
            // 保留两位小数
            DecimalFormat formater = new DecimalFormat("0.00");
            // 四舍五入
            formater.setRoundingMode(RoundingMode.HALF_UP);
            // 格式化完成之后得出结果
            String formatNum = formater.format(decimal);
            financeAbutmentDto.setMoney(formatNum);
        } else {
            financeAbutmentDto.setMoney("0");
        }

        if (allxq > 0) {
            String str = Integer.toString(allxq);
            BigDecimal bigDecimal = new BigDecimal(str);
            // 转换为万元（除以10000）
            BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
            // 保留两位小数
            DecimalFormat formater = new DecimalFormat("0.00");
            // 四舍五入
            formater.setRoundingMode(RoundingMode.HALF_UP);
            // 格式化完成之后得出结果
            String formatNum = formater.format(decimal);
            financeAbutmentDto.setDemandFunds(formatNum);
        } else {
            financeAbutmentDto.setDemandFunds("0");
        }
        //融资需求额
        return financeAbutmentDto;
    }

    public List<Object> getPie(Integer level) {

        int artificialIntelligence = 0;//人工智能
        int photoelectricManufacturing = 0;//光电制造
        int bioengineering = 0;//生物工程
        int cottonSpinning = 0;//化纤棉纺
        int automobileManufacturing = 0;//汽车制造
        int other = 0;//其他
        int smartCity = 0;//智慧城市
        int soft = 0;//信息软件
        int conserveEnergy = 0;//节能减排
        int ruralVitalization = 0;//乡村振兴
        int culturalTourism = 0;//文化旅游
        int newMaterials = 0;//新材料
        int newTechnique = 0;//新技术
        int newEnergy = 0;//新能源

        if (level == 1) { //职创项目库 1    等于 1+2+3
            artificialIntelligence = projectRepository.findbyLevelAndRacedomain("人工智能", 1) + projectRepository.findbyLevelAndRacedomain("人工智能", 2) + projectRepository.findbyLevelAndRacedomain("人工智能", 3);
            photoelectricManufacturing = projectRepository.findbyLevelAndRacedomain("光电制造", 1) + projectRepository.findbyLevelAndRacedomain("光电制造", 2) + projectRepository.findbyLevelAndRacedomain("光电制造", 3);
            bioengineering = projectRepository.findbyLevelAndRacedomain("生物工程", 1) + projectRepository.findbyLevelAndRacedomain("生物工程", 2) + projectRepository.findbyLevelAndRacedomain("生物工程", 3);
            cottonSpinning = projectRepository.findbyLevelAndRacedomain("化纤棉纺", 1) + projectRepository.findbyLevelAndRacedomain("化纤棉纺", 2) + projectRepository.findbyLevelAndRacedomain("化纤棉纺", 3);
            automobileManufacturing = projectRepository.findbyLevelAndRacedomain("汽车制造", 1) + projectRepository.findbyLevelAndRacedomain("汽车制造", 2) + projectRepository.findbyLevelAndRacedomain("汽车制造", 3);
            other = projectRepository.findbyLevelAndRacedomain("其他", 1) + projectRepository.findbyLevelAndRacedomain("其他", 2) + projectRepository.findbyLevelAndRacedomain("其他", 3) +
                    projectRepository.findbyLevelAndRacedomainIsnull(1) + projectRepository.findbyLevelAndRacedomainIsnull(2) + projectRepository.findbyLevelAndRacedomainIsnull(3) +
                    projectRepository.findbyLevelAndRacedomainNoContains(1) + projectRepository.findbyLevelAndRacedomainNoContains(2) + projectRepository.findbyLevelAndRacedomainNoContains(3);
            smartCity = projectRepository.findbyLevelAndRacedomain("智慧城市", 1) + projectRepository.findbyLevelAndRacedomain("智慧城市", 2) + projectRepository.findbyLevelAndRacedomain("智慧城市", 3);
            soft = projectRepository.findbyLevelAndRacedomain("信息软件", 1) + projectRepository.findbyLevelAndRacedomain("信息软件", 2) + projectRepository.findbyLevelAndRacedomain("信息软件", 3);
            conserveEnergy = projectRepository.findbyLevelAndRacedomain("节能减排", 1) + projectRepository.findbyLevelAndRacedomain("节能减排", 2) + projectRepository.findbyLevelAndRacedomain("节能减排", 3);
            ruralVitalization = projectRepository.findbyLevelAndRacedomain("乡村振兴", 1) + projectRepository.findbyLevelAndRacedomain("乡村振兴", 2) + projectRepository.findbyLevelAndRacedomain("乡村振兴", 3);
            culturalTourism = projectRepository.findbyLevelAndRacedomain("文化旅游", 1) + projectRepository.findbyLevelAndRacedomain("文化旅游", 2) + projectRepository.findbyLevelAndRacedomain("文化旅游", 3);
            newMaterials = projectRepository.findbyLevelAndRacedomain("新材料", 1) + projectRepository.findbyLevelAndRacedomain("新材料", 2) + projectRepository.findbyLevelAndRacedomain("新材料", 3);
            newTechnique = projectRepository.findbyLevelAndRacedomain("新技术", 1) + projectRepository.findbyLevelAndRacedomain("新技术", 2) + projectRepository.findbyLevelAndRacedomain("新技术", 3);
            newEnergy = projectRepository.findbyLevelAndRacedomain("新能源", 1) + projectRepository.findbyLevelAndRacedomain("新能源", 2) + projectRepository.findbyLevelAndRacedomain("新能源", 3);


        } else if (level == 2) { //优秀项目库   等于 2+3

            artificialIntelligence = projectRepository.findbyLevelAndRacedomain("人工智能", 2) + projectRepository.findbyLevelAndRacedomain("人工智能", 3);
            photoelectricManufacturing = projectRepository.findbyLevelAndRacedomain("光电制造", 2) + projectRepository.findbyLevelAndRacedomain("光电制造", 3);
            bioengineering = projectRepository.findbyLevelAndRacedomain("生物工程", 2) + projectRepository.findbyLevelAndRacedomain("生物工程", 3);
            cottonSpinning = projectRepository.findbyLevelAndRacedomain("化纤棉纺", 2) + projectRepository.findbyLevelAndRacedomain("化纤棉纺", 3);
            automobileManufacturing = projectRepository.findbyLevelAndRacedomain("汽车制造", 2) + projectRepository.findbyLevelAndRacedomain("汽车制造", 3);
            other = projectRepository.findbyLevelAndRacedomain("其他", 2) + projectRepository.findbyLevelAndRacedomain("其他", 3) +
                    projectRepository.findbyLevelAndRacedomainIsnull(2) + projectRepository.findbyLevelAndRacedomainIsnull(3) +
                    projectRepository.findbyLevelAndRacedomainNoContains(2) + projectRepository.findbyLevelAndRacedomainNoContains(3);
            smartCity = projectRepository.findbyLevelAndRacedomain("智慧城市", 2) + projectRepository.findbyLevelAndRacedomain("智慧城市", 3);
            soft = projectRepository.findbyLevelAndRacedomain("信息软件", 2) + projectRepository.findbyLevelAndRacedomain("信息软件", 3);
            conserveEnergy = projectRepository.findbyLevelAndRacedomain("节能减排", 2) + projectRepository.findbyLevelAndRacedomain("节能减排", 3);
            ruralVitalization = projectRepository.findbyLevelAndRacedomain("乡村振兴", 2) + projectRepository.findbyLevelAndRacedomain("乡村振兴", 3);
            culturalTourism = projectRepository.findbyLevelAndRacedomain("文化旅游", 2) + projectRepository.findbyLevelAndRacedomain("文化旅游", 3);
            newMaterials = projectRepository.findbyLevelAndRacedomain("新材料", 2) + projectRepository.findbyLevelAndRacedomain("新材料", 3);
            newTechnique = projectRepository.findbyLevelAndRacedomain("新技术", 2) + projectRepository.findbyLevelAndRacedomain("新技术", 3);
            newEnergy = projectRepository.findbyLevelAndRacedomain("新能源", 2) + projectRepository.findbyLevelAndRacedomain("新能源", 3);

        } else {

            artificialIntelligence = projectRepository.findbyLevelAndRacedomain("人工智能", level);
            photoelectricManufacturing = projectRepository.findbyLevelAndRacedomain("光电制造", level);
            bioengineering = projectRepository.findbyLevelAndRacedomain("生物工程", level);
            cottonSpinning = projectRepository.findbyLevelAndRacedomain("化纤棉纺", level);
            automobileManufacturing = projectRepository.findbyLevelAndRacedomain("汽车制造", level);
            other = projectRepository.findbyLevelAndRacedomain("其他", level) + projectRepository.findbyLevelAndRacedomainIsnull(level);
            smartCity = projectRepository.findbyLevelAndRacedomain("智慧城市", level);
            soft = projectRepository.findbyLevelAndRacedomain("信息软件", level);
            conserveEnergy = projectRepository.findbyLevelAndRacedomain("节能减排", level);
            ruralVitalization = projectRepository.findbyLevelAndRacedomain("乡村振兴", level);
            culturalTourism = projectRepository.findbyLevelAndRacedomain("文化旅游", level);
            newMaterials = projectRepository.findbyLevelAndRacedomain("新材料", level);
            newTechnique = projectRepository.findbyLevelAndRacedomain("新技术", level);
            newEnergy = projectRepository.findbyLevelAndRacedomain("新能源", level);
        }


        List<Object> list = new ArrayList<Object>();

        LinkedHashMap<String, Object> jsonMap1 = new LinkedHashMap<String, Object>();
        jsonMap1.put("name", "人工智能");
        jsonMap1.put("value", artificialIntelligence);
        list.add(jsonMap1);

        LinkedHashMap<String, Object> jsonMap2 = new LinkedHashMap<String, Object>();
        jsonMap2.put("name", "光电制造");
        jsonMap2.put("value", photoelectricManufacturing);
        list.add(jsonMap2);

        LinkedHashMap<String, Object> jsonMap3 = new LinkedHashMap<String, Object>();
        jsonMap3.put("name", "生物工程");
        jsonMap3.put("value", bioengineering);
        list.add(jsonMap3);

        LinkedHashMap<String, Object> jsonMap4 = new LinkedHashMap<String, Object>();
        jsonMap4.put("name", "化纤棉纺");
        jsonMap4.put("value", cottonSpinning);
        list.add(jsonMap4);

        LinkedHashMap<String, Object> jsonMap5 = new LinkedHashMap<String, Object>();
        jsonMap5.put("name", "汽车制造");
        jsonMap5.put("value", automobileManufacturing);
        list.add(jsonMap5);

        LinkedHashMap<String, Object> jsonMap6 = new LinkedHashMap<String, Object>();
        jsonMap6.put("name", "其他");
        jsonMap6.put("value", other);
        list.add(jsonMap6);

        LinkedHashMap<String, Object> jsonMap7 = new LinkedHashMap<String, Object>();
        jsonMap7.put("name", "智慧城市");
        jsonMap7.put("value", smartCity);
        list.add(jsonMap7);

        LinkedHashMap<String, Object> jsonMap8 = new LinkedHashMap<String, Object>();
        jsonMap8.put("name", "信息软件");
        jsonMap8.put("value", soft);
        list.add(jsonMap8);

        LinkedHashMap<String, Object> jsonMap9 = new LinkedHashMap<String, Object>();
        jsonMap9.put("name", "节能减排");
        jsonMap9.put("value", conserveEnergy);
        list.add(jsonMap9);

        LinkedHashMap<String, Object> jsonMap10 = new LinkedHashMap<String, Object>();
        jsonMap10.put("name", "乡村振兴");
        jsonMap10.put("value", ruralVitalization);
        list.add(jsonMap10);

        LinkedHashMap<String, Object> jsonMap11 = new LinkedHashMap<String, Object>();
        jsonMap11.put("name", "文化旅游");
        jsonMap11.put("value", culturalTourism);
        list.add(jsonMap11);

        LinkedHashMap<String, Object> jsonMap12 = new LinkedHashMap<String, Object>();
        jsonMap12.put("name", "新材料");
        jsonMap12.put("value", newMaterials);
        list.add(jsonMap12);

        LinkedHashMap<String, Object> jsonMap13 = new LinkedHashMap<String, Object>();
        jsonMap13.put("name", "新技术");
        jsonMap13.put("value", newTechnique);
        list.add(jsonMap13);

        LinkedHashMap<String, Object> jsonMap14 = new LinkedHashMap<String, Object>();
        jsonMap14.put("name", "新能源");
        jsonMap14.put("value", newEnergy);
        list.add(jsonMap14);
        return list;
    }
}